﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace PowerView.Page
{
    public partial class AFC_Form : Form
    {
        const byte QC_Protocol = 0x30;
        const byte AFC_Protocol = 0x40;
        byte[] data_buf = new byte[128];
        List<byte> AFC_Capabilities = new List<byte>();
        public AFC_Form()
        {
            InitializeComponent();
        }

        public void CMD_Proc(byte[] cmd)
        {
            switch(cmd[3])
            {
                case 0x00:
                    AFC_Capabilities_Proc(cmd);
                    break;
                case 0x01:
                    break;
                case 0x02:
                    if (cmd[4] == 0)
                    {
                        Recv_Data_textBox.Text = MiddleWare.ByteArrayToString(cmd.Skip(6).ToArray(), cmd[5]);
                    }
                    break;
                case 0x10:
                    FCP_Capabilities_Proc(cmd);
                    break;
            }
        }

        // 解析适配器的 AFC 供电档位
        private void AFC_Capabilities_Proc(byte[] cmd)
        {
            AFC_listBox.Items.Clear();
            AFC_Capabilities.Clear();
            for (int i = 0; i < cmd[5]; i++)
            {
                AFC_Capabilities.Add(cmd[6 + i]);
                AFC_listBox.Items.Add("[0x" + cmd[6 + i].ToString("X2") + "] " + 
                                        (5 + (cmd[6 + i] >> 4)).ToString() + "V " + 
                                        (0.75 + (cmd[6 + i] & 0x0F) * 0.15).ToString() + "A");
            }
        }
        private void AFC_ShakeHand_Btn_Click(object sender, EventArgs e)
        {
            MiddleWare.Write_CMD(0xff, AFC_Protocol, 0x00, data_buf, 0);
        }

        private void Send_Data_Btn_Click(object sender, EventArgs e)
        {
            byte[] _data = MiddleWare.HexStringToByteArray(Send_Data_textBox.Text);

            if(CRC_checkBox.Checked)
            {
                byte[] crc_p = MiddleWare.HexStringToByteArray(CRC_textBox.Text);

                if (crc_p.Length != 0)
                {
                    byte crc = MiddleWare.CRC8_Calculate(_data, _data.Length, crc_p[0]);
                    _data = _data.ToList().Append(crc).ToArray();
                }
            }
            MiddleWare.Write_CMD(0xff, AFC_Protocol, 0x02, _data, (byte)_data.Length);
        }

        private void AFC_listBox_DoubleClick(object sender, EventArgs e)
        {
            MiddleWare.Write_CMD(0xff, AFC_Protocol, 0x01, new byte[] { AFC_Capabilities.ElementAt(AFC_listBox.SelectedIndex) }, 1);
        }

        // 解析适配器的 FCP 供电档位
        private void FCP_Capabilities_Proc(byte[] cmd)
        {
            FCP_listBox.Items.Clear();
            if (cmd[5] != 3) // 数据长度不对
            {
                MiddleWare.log_tBox.AppendText("FCP 返回数据错误！！！\r\n");
                return;
            }
            switch (cmd[7])
            {
                case 0x01:
                    FCP_listBox.Items.Add("5V ");
                    FCP_listBox.Items.Add("9V ");
                    break;
                case 0x02:
                    FCP_listBox.Items.Add("5V ");
                    FCP_listBox.Items.Add("9V ");
                    FCP_listBox.Items.Add("12V ");
                    break;
            }
        }

        private void FCP_ShakeHand_Btn_Click(object sender, EventArgs e)
        {
            MiddleWare.Write_CMD(0xff, AFC_Protocol, 0x10, data_buf, 0);
        }

        private void FCP_listBox_DoubleClick(object sender, EventArgs e)
        {
            MiddleWare.Write_CMD(0xff, AFC_Protocol, (byte)(FCP_listBox.SelectedIndex + 0x11), null, 0);
        }

        private void SCP_ShakeHand_Btn_Click(object sender, EventArgs e)
        {
            MiddleWare.Write_CMD(0xff, AFC_Protocol, 0x20, data_buf, 0);
        }

        private void SCP_VISet_Btn_Click(object sender, EventArgs e)
        {
            float Vout = -1, Iout = -1;
            UInt16 Vout_data, Iout_data;
            byte[] _data = new byte[16];

            float.TryParse(SCP_VSet_textBox.Text, out Vout);
            float.TryParse(SCP_ISet_textBox.Text, out Iout);

            Vout_data = (UInt16)(Vout * 1000);
            Iout_data = (UInt16)(Iout * 1000);

            _data[0] = 0x1B; // 多字节写寄存器
            _data[1] = 0xB8; // 寄存器地址
            _data[2] = 0x04; // 数据长度
            _data[3] = (byte)(Vout_data >>8 ); // 电压 
            _data[4] = (byte)(Vout_data & 0xFF);
            _data[5] = (byte)(Iout_data >> 8); // 电流
            _data[6] = (byte)(Iout_data & 0xFF);
            _data[7] = MiddleWare.CRC8_Calculate(_data, 7, 0x39); // CRC

            MiddleWare.Write_CMD(0xff, AFC_Protocol, 0x02, _data, 8);
        }
    }
}
